<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/22 0022
 * Time: 10:10
 */

namespace app\admin\controller;


use think\Controller;
use think\Config;
use think\Loader;
use think\Db;

class ArticlecateController extends BaseController
{
    //主页
    public function index(){
        if (request()->isAjax()){
            $page=input("param.page");
            $limit=input("param.limit",100);
            $sum=($page-1)*$limit;
            $keyword=input("param.keyword");
            $where=[];
            if ($keyword){
                $where["cate_name"]=["like","%{$keyword}%"];
            }
            $data=Db::name("article_cate")->where($where)->order("id ASC")->limit("$sum,$limit")->select();
            $num=Db::name("article_cate")->where($where)->order("id ASC")->select();
            $list=artle($data);
            $result["code"]=0;
            $result["msg"]="";
            $result["count"]=count($num);
            $result["data"]=$list;
            return json($result);
        }
        return $this->fetch("index");
    }
    /*
     * 添加
     */
    public function add(){
        if (request()->isAjax()){
            $list["pid"]=input("param.pid");
            $list["cate_name"]=input("param.cate_name");
            $list["sort"]=input("param.sort");
            $find=Db::name("article_cate")->where(["cate_name"=>$list["cate_name"]])->find();
            if ($find){
                return json(["code"=>0,"msg"=>"该分类已存在"]);
            }
            $result=Db::name("article_cate")->insert($list);
            if ($result){
                return json(["code"=>1,"msg"=>"添加成功"]);
            }else{
                return json(["code"=>0,"msg"=>"添加失败"]);
            }
        }
        //读取一级 分类
        $find=Db::name("article_cate")->where("pid","=",0)->select();
        $this->assign("find",$find);
        return $this->fetch("add");

    }

    /*
     * 编辑
     */
    public function edit(){

        $id=input("param.id");
        if (request()->isPost()) {
            $data["id"]=input("param.id");
            $data["cate_name"]=input("param.cate_name");
            $data["sort"]=input("param.sort");
            $data["pid"]=input("param.pid");
            $result=Db::name("article_cate")->where(["id"=>$data["id"]])->update($data);
            if ($result){
                return json(["code"=>1,"msg"=>"编辑成功"]);
            }else{
                return json(["code"=>0,"msg"=>"编辑失败"]);
            }
        }
        $list=Db::name("article_cate")->where(["pid"=>0])->select();
        $find=Db::name("article_cate")->where(["id"=>$id])->find();
        $this->assign("find",$find);
        $this->assign("list",$list);
        return $this->fetch("edit");

    }




    /*
     * 删除
     */
    public function del(){
        $id=input("param.id");
        $find=Db::name("article_cate")->where(["pid"=>$id])->delete();
        if ($find){
            return json(["code"=>0,"msg"=>"删除失败,该分类才有子分类"]);
        }
        $result=Db::name("article_cate")->where(["id"=>$id])->delete();
        if ($result){
            writelog("删除分类成功");
            return json(["code"=>1,"msg"=>"删除成功"]);
        }else{
            return json(["code"=>0,"msg"=>"删除失败"]);
        }
    }
}